package com.example.liqm.greendao.db.test;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.liqm.greendao.model.DaoMaster;
import com.example.liqm.greendao.model.DaoSession;
import com.example.liqm.greendao.ui.MyAPP;

/**
 * Created by liqm on 2017/7/9.
 */

public class GreenDaoUtils {
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    private static  GreenDaoUtils greenDaoUtils;

    private  GreenDaoUtils(){

    }
    public static  GreenDaoUtils getSingleTon(){
        if (greenDaoUtils ==null){
            greenDaoUtils = new GreenDaoUtils();
        }
        return greenDaoUtils;
    }

    private void initGreenDao(){
        mHelper = new DaoMaster.DevOpenHelper(MyAPP.getContext(),"test-db",null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }
    public DaoSession getmDaoSession(){
        if (mDaoMaster ==null){
            initGreenDao();
        }
        return mDaoSession;
    }
    public SQLiteDatabase getDb(){
        if (db==null){
            initGreenDao();
        }
        return db;
    }

}
